//
//  BaseNaviViewController.m
//  ubene
//
//  Created by 孔晓寅 on 15/10/22.
//  Copyright © 2015年 孔晓寅. All rights reserved.
//

#import "BaseNaviViewController.h"
#import "UINavigationBar+Awesome.h"

@interface BaseNaviViewController ()

@end

@implementation BaseNaviViewController
@synthesize searchBar,rightImage,rightLabel,numLabel;

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.modalPresentationCapturesStatusBarAppearance = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.navigationController setNavigationBarHidden:NO];
    [self.view setBackgroundColor:[utils getBackGroupColor]];
    
    self.navigationController.navigationBar.titleTextAttributes = @{UITextAttributeTextColor: [UIColor whiteColor],UITextAttributeFont : [UIFont systemFontOfSize:18]};

    //    UIStatusBarStyleLightContent
    [self.navigationController.navigationBar setBarStyle:UIStatusBarStyleLightContent];//状态栏白色
    
    
    NSArray *viewControllerArray = [self.navigationController viewControllers];
    long previousViewControllerIndex = [viewControllerArray indexOfObject:self] - 1;
    UIViewController *previous;
    if (previousViewControllerIndex >= 0) {
        previous = [viewControllerArray objectAtIndex:previousViewControllerIndex];
        previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                     initWithTitle:@""
                                                     style:UIBarButtonItemStylePlain
                                                     target:self
                                                     action:nil];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController.navigationBar lt_setBackgroundColor:[utils getThemeColor]];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    [self.navigationController.navigationBar lt_reset];
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 状态栏搜索框
-(void)setCustomSearchBar:(id)target placeholder:(NSString*)placeholder{
    
    searchBar = [[UITextField alloc]initWithFrame:CGRectMake(10, 0,self.view.frame.size.width , 22)];
    searchBar.backgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1];
    searchBar.placeholder = placeholder;
    searchBar.font =[UIFont systemFontOfSize:10];
    searchBar.layer.cornerRadius = 11;
    [searchBar.layer setBorderColor:[UIColor clearColor].CGColor];//边框颜色
//    [searchBar.layer setBorderWidth:1];   //边框宽度
    [searchBar.layer setMasksToBounds:YES];
    searchBar.borderStyle = UITextBorderStyleRoundedRect;
    searchBar.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;//设置其输入内容竖直居中
    searchBar.returnKeyType = UIReturnKeySearch;
    searchBar.clearButtonMode = UITextFieldViewModeWhileEditing;
    searchBar.clearsOnBeginEditing = YES;
    searchBar.textColor = [utils getTextColorGray];
//    searchBar.leftView = [[UIView alloc]initWithFrame:CGRectMake(20, 0, 22, 0)];
//    searchBar.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sy_ss"]];
    searchBar.leftView=image;
    searchBar.leftViewMode = UITextFieldViewModeAlways;
    
    searchBar.delegate = target;
//    searchBar.placeholder = placeholder;
    UIColor *color = [utils getTextColorGray];
    searchBar.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: color}];
    
    [self.navigationItem setTitleView:searchBar];
}

#pragma mark 设置左边按钮
-(void)setCustomLeftButton:(id)target action:(SEL)action img:(NSString *)imagename{
//    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"左按钮" style:UIBarButtonItemStylePlain target:self action:action];
//    
//    [self.navigationItem setLeftBarButtonItem:leftButton];
    
//    UIBarButtonItem *leftButton=[[UIBarButtonItem alloc]init];
//    leftButton.title=@"";
//    leftButton.image=[UIImage imageNamed:imagename];
//    [leftButton setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//    [self.navigationItem setLeftBarButtonItem:leftButton];
    
    UIImage *image = [UIImage imageNamed:imagename];
    UIImageView *headIcon = [[UIImageView alloc]initWithImage:image ];
    headIcon.frame = CGRectMake(0, 0, 88, 35);
    headIcon.bounds = CGRectMake(10, 0, 88, 25);
    headIcon.contentMode = UIViewContentModeScaleAspectFill;
    
    UITapGestureRecognizer *tapHeadIcon = [[UITapGestureRecognizer alloc]initWithTarget:self action:action];
    [headIcon addGestureRecognizer:tapHeadIcon];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:headIcon];//设置navigationbar左边按钮
}

#pragma mark 设置左边按钮
-(void)setCustomLeftButton_defualt:(id)target action:(SEL)action img:(NSString *)imagename{
    
    UIImage *image = [UIImage imageNamed:imagename];
    UIImageView *headIcon = [[UIImageView alloc]initWithImage:image ];
    headIcon.frame = CGRectMake(0, 0, 40, 40);
    headIcon.bounds = CGRectMake(0, 0, 40, 40);
    headIcon.contentMode = UIViewContentModeScaleAspectFit;
    
    UITapGestureRecognizer *tapHeadIcon = [[UITapGestureRecognizer alloc]initWithTarget:self action:action];
    [headIcon addGestureRecognizer:tapHeadIcon];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -17;
    self.navigationItem.leftBarButtonItems = @[negativeSpacer, [[UIBarButtonItem alloc] initWithCustomView:headIcon]];
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:headIcon];//设置navigationbar左边按钮
}

#pragma mark 设置右边按钮
-(void)setCustomRightButton:(id)target action:(SEL)action img:(NSString *)imagename{

    UIImage *image = [UIImage imageNamed:imagename];
    rightImage = [[UIImageView alloc]initWithImage:image ];
    rightImage.frame = CGRectMake(0, 0, 35, 30);
    rightImage.bounds = CGRectMake(0, 0, 35, 30);
    rightImage.contentMode = UIViewContentModeScaleAspectFit;
    rightImage.tag = 0;
    
    UITapGestureRecognizer *tapHeadIcon = [[UITapGestureRecognizer alloc]initWithTarget:self action:action];
    [rightImage addGestureRecognizer:tapHeadIcon];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightImage];//设置navigationbar左边按钮
}

-(void)setCustomRightButtonWithNum:(id)target action:(SEL)action img:(NSString *)imagename{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 35)];
    
//    UIImage *image = [UIImage imageNamed:imagename];
    rightImage = [[UIImageView alloc]init];
    [rightImage sd_setImageWithURL:[NSURL URLWithString:imagename] placeholderImage:[UIImage imageNamed:@"icon_xiaoxi_2x"] options:SDWebImageRefreshCached|SDWebImageContinueInBackground|SDWebImageRetryFailed];
    
    rightImage.frame = CGRectMake(0, 0, 35, 30);
    rightImage.bounds = CGRectMake(0, 0, 35, 30);
    rightImage.contentMode = UIViewContentModeScaleAspectFit;
    rightImage.tag = 0;
    [view addSubview:rightImage];
    
    numLabel = [utils getLabel:10 color:[UIColor whiteColor]];
    [numLabel setBackgroundColor:[UIColor colorWithRed:250.0/255.0 green:105.0/255.0 blue:106.0/255.0 alpha:1.0]];
    numLabel.textAlignment = NSTextAlignmentCenter;
    numLabel.frame = CGRectMake(view.frame.size.width - 20, view.frame.size.height-20, 20, 12);
    numLabel.layer.cornerRadius = 6;
    numLabel.layer.masksToBounds = YES;
    numLabel.text = @"";
    [numLabel setHidden:YES];
    [view addSubview:numLabel];
    
    UITapGestureRecognizer *tapHeadIcon = [[UITapGestureRecognizer alloc]initWithTarget:self action:action];
    [view addGestureRecognizer:tapHeadIcon];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    
}

-(void)setNum:(NSUInteger)num{
    num = num;
    if (num>0) {
        [numLabel setHidden:NO];
        numLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)num];
    }else{
        [numLabel setHidden:YES];
    }
}

#pragma mark 设置右边文字
-(void)setCustomRightText:(id)target action:(SEL)action text:(NSString *)text{
    
    rightLabel = [utils getLabel:18 color:[UIColor whiteColor]];
    rightLabel.text = text;
    [rightLabel sizeToFit];
    rightLabel.frame = CGRectMake(0, 0, rightLabel.frame.size.width, 44);
    //    button.bounds = CGRectMake(0, 0, 40, 40);
    rightLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapHeadIcon = [[UITapGestureRecognizer alloc]initWithTarget:self action:action];
    [rightLabel addGestureRecognizer:tapHeadIcon];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightLabel];//设置navigationbar左边按钮
}

-(void)setCustomRightBT:(id)target action:(SEL)action img:(NSString *)imagename text:(NSString *)text{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];
    
    UIImage *image = [UIImage imageNamed:imagename];
    rightImage = [[UIImageView alloc]initWithImage:image ];
    rightImage.frame = CGRectMake(65, 0, 30, 60);
//    rightImage.bounds = CGRectMake(0, 0, 20, 20);
    rightImage.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:rightImage];
    
    rightLabel = [utils getLabel:11 color:[UIColor whiteColor]];
    rightLabel.textAlignment = NSTextAlignmentRight;
    rightLabel.numberOfLines = 0;
    rightLabel.text = text;
    [rightLabel sizeToFit];
    rightLabel.frame = CGRectMake(0, 0, 60, 60);
    [view addSubview:rightLabel];
    
    UITapGestureRecognizer *tapHeadIcon = [[UITapGestureRecognizer alloc]initWithTarget:self action:action];
    [view addGestureRecognizer:tapHeadIcon];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];//设置navigationbar左边按钮
}
@end
